#Include <..\include\Common_For_PageManager.au3>
#Include <GuiButton.au3>
#Include <GuiTab.au3>

AutoItSetOption("MustDeclareVars", 1)   ;Variables must be pre-declared.

Dim Const $sCorrectResultPath 			= "C:\Save As\Correct Result\"   ;Corretc result file path.
Dim Const $sTestResultPath 				= "C:\Save As\Perform Result\"   ;Proformed result file path.
Dim Const $iNumber 						= Common_GetNumberInExcelSheet($oExcel,$sSaveAsFunctionName)  
Dim Const $SaveAsFunctionName           = $sSaveAsFunctionName
Dim Const $iCurrentSheetTotalLineNumber = $iNumber[0]   ;Get all number of rows in current excel sheet.
Dim Const $sFunctionFolderPath 			= "C:\Save As\"  
Dim Const $sSampleFolderPath 			= "C:\Save As\Sample\"  
;Dim Const $CommonWindowCtrlInfro 		= '[Class:#32770]'
Dim Const $CommonWindowCtrlInfro 		= ''
Dim Const $sSaveAsTypeCtrlInfo 			= Number(ReadIniKeyValue($sPCodeIniPath, $sSectionNameThumbanil, 'SaveAsType' ))
Global Const $sExcelSheetName = 'Save As'
Global Const $iRescolumn = 14
Global  $sCusMsg = ''
Global  $sFailLogMsg = ''
;Key Value:
Dim Const $sEditMenuHotKey 				= ReadIniKeyValue($sPCodeIniPath, $sSectionNameThumbanil, 'EditMenuHotKey')
Dim Const $sToolsMenuHotKey 			= ReadIniKeyValue($sPCodeIniPath, $sSectionNameThumbanil, 'ToolsMenu')
Dim Const $sViewMenuHotKey 				= ReadIniKeyValue($sPCodeIniPath, $sSectionNameThumbanil, 'ViewMenuHotKey')
Dim Const $sFileMenuHotKey 				= ReadIniKeyValue($sPCodeIniPath, $sSectionNameThumbanil, 'FileMenuHotKey')
Dim Const $SaveAsMenu 					= ReadIniKeyValue($sPCodeIniPath, $sSectionNameSaveAs, 'SaveAsMenu')
Dim Const $SaveAsContextMenu 			= ReadIniKeyValue($sPCodeIniPath, $sSectionNameSaveAs, 'SaveAsContextMenu')
Dim Const $sAnnotationBarCtrlInfo 		= ReadIniKeyValue($sPCodeIniPath, $sSectionNameThumbanil, 'AnnotationBar')
Dim Const $sSettingBtnInSaveAsDlg 		= Number(ReadIniKeyValue($sPCodeIniPath, $sSectionNameThumbanil, 'SettingBtnInSaveAsDlg'))
Dim Const $sSaveAnnoToPMCtrlInfo 		= Number(ReadIniKeyValue($sPCodeIniPath, $sSectionNameThumbanil, 'SaveAnnoToPM'))
Dim Const $sSaveAnnoToAllAPCtrlInfo 	= Number(ReadIniKeyValue($sPCodeIniPath, $sSectionNameThumbanil, 'SaveAnnoToAllAP'))
Dim Const $sNotSaveAnnoCtrlInfo 		= Number(ReadIniKeyValue($sPCodeIniPath, $sSectionNameThumbanil, 'NotSaveAnno'))
Dim Const $sTileButtonInToolBarCtrlInfo = Number(ReadIniKeyValue($sPCodeIniPath, $sSectionNameThumbanil, 'TileButtonInToolBar'))


Func DeleteLastTimeSavedFile($sName)
	 #cs
	If FileExists($sName) Then
		FileDelete($sName)
	EndIf
	#ce
EndFunc

Func SelectOneFileInListiew()
	MouseClick('left',370, 106)
EndFunc

Func SetStampAnnotation()
	;Add Anonation
	MouseClick('left', 1255,285)
	Send('{Down}')
	Sleep(300)
	Send('{ENTER}')
	MouseClick('left', 426,261)
	Common_SleepTime(1)	
EndFunc

Func JPGcompression($Value)
	ControlClick($CommonWindowCtrlInfro, "", 1095)   ; Click propertiy button.
	Sleep(1000)
	ControlFocus($CommonWindowCtrlInfro, "", 1004)
	Switch $Value   
	Case "COMPRESSION"
		For $i = 0 to 50
			Send("{LEFT}")
		Next
		Sleep(500)
	Case "QUALITY"
		For $i = 0 to 50
			Send("{RIGHT}")
		Next
		Sleep(500)
	EndSwitch
	ControlClick($CommonWindowCtrlInfro, "", 1)
	Sleep(1000)
EndFunc


Func OpenSaveAsDlgFromFileMenu()
	OpenMenu("File")
	Send($SaveAsMenu)
	Common_SleepTime(4)
	WinWaitActive($CommonWindowCtrlInfro, '', 5)
	WinActivate($CommonWindowCtrlInfro)
	Common_SleepTime(3)
EndFunc

Func SaveAnnotationOptionForSaveAs($sOption)
	Local $sRe = ControlClick($CommonWindowCtrlInfro, "", $sSettingBtnInSaveAsDlg) 
	Sleep(1000)
	If Not @error Or $sRe = 0 Then
	Else
		Return @error
	EndIf
	Switch $sOption
	Case "Save to PM"
		Local $sRe = ControlClick($CommonWindowCtrlInfro, "", $sSaveAnnoToPMCtrlInfo) 
		If Not @error Or $sRe = 0 Then
			Sleep(500)
		Else
			Return @error
		EndIf	
	Case "Save to All AP"
		Local $sRe = ControlClick($CommonWindowCtrlInfro, "", $sSaveAnnoToAllAPCtrlInfo) 
		If Not @error Or $sRe = 0 Then
			Sleep(500)
		Else
			Return @error
		EndIf	
	Case "Not Save Annotation"
		Local $sRe = ControlClick($CommonWindowCtrlInfro, "", $sNotSaveAnnoCtrlInfo) 
		If Not @error Or $sRe = 0 Then
			Sleep(500)
		Else
			Return @error
		EndIf		
	EndSwitch
	Sleep(500)
	Local $sRe = ControlClick($CommonWindowCtrlInfro, "", 1)   ; Click "OK" button.
	Sleep(500)
	If Not @error Or $sRe = 0 Then
	Else
		Return @error
	EndIf	
EndFunc

Func OpenSaveAsDlgFromContextMenu()
	Common_SleepTime(2)	
	Send($SaveAsContextMenu)
	Common_SleepTime(1)	
;	Send($SaveAsContextMenu)
	Send('{Enter}')
	Common_SleepTime(10)
EndFunc

Func OpenSaveAsDlgByCtrlS()
	Send("^s")
	Common_SleepTime(10)
	WinWaitActive($CommonWindowCtrlInfro, '', 5)
	WinActivate($CommonWindowCtrlInfro)
EndFunc

Func SetSaveAsFileName($sName)
	ControlFocus($CommonWindowCtrlInfro, "", 1152)
	ControlSetText($CommonWindowCtrlInfro, "", 1152, $sName)
	Common_SleepTime(1)
	ControlClick($CommonWindowCtrlInfro, "", "Button2")
	Common_SleepTime(18)
	If WinExists("OCR") Then
		WinWaitClose("OCR", "", 30)
		Common_SleepTime(4)
	EndIf
EndFunc

Func SetSaveAsFileNameInPageView($sName)
	ControlFocus($CommonWindowCtrlInfro, "", 1148)
	ControlSetText($CommonWindowCtrlInfro, "", 1148, $sName)
	Common_SleepTime(1)
	ControlClick($CommonWindowCtrlInfro, "", "Button2")
	Common_SleepTime(18)
	If WinExists("OCR") Then
		WinWaitClose("OCR", "", 30)
		Common_SleepTime(4)
	EndIf
EndFunc

Func OpenMenu($sMenu)
	Switch $sMenu
	Case "Edit"
		Send("!" & $sEditMenuHotKey)	
		Common_SleepTime(2)	
	Case "Tools"
		Send("!" & $sToolsMenuHotKey)	
		Common_SleepTime(2)	
	Case "View"
		Send("!" & $sViewMenuHotKey)	
		Common_SleepTime(2)	
	Case "File"
		Send("!" & $sFileMenuHotKey)	
		Common_SleepTime(2)
	EndSwitch	
EndFunc

Func DeletePerformResult()
	Local $FileList = _FileListToArray ($sTestResultPath,"*", 0)
	If Not @error Then
		If $FileList[0] >= 1 Then
			For $i = 1 to $FileList[0]
				FileDelete ($sTestResultPath & $FileList[$i])
			Next
			Return 1
		EndIf
	EndIf
	Return @error
EndFunc

Func InitializeStatusForSaveAs()
	;Delete all files in "C:\Save As\"
	Local $FileList = _FileListToArray ($sFunctionFolderPath,"*", 1)
	If Not @error Then
		If $FileList[0] >= 1 Then
			For $i = 1 to $FileList[0]
				FileDelete ($sFunctionFolderPath & $FileList[$i])
			Next
			Return 1
		EndIf
	EndIf
	Return @error
EndFunc

Func SetSaveAsFormat($Format)
	ControlClick("[CLASS:#32770]", "FolderView", $sSaveAsTypeCtrlInfo)
	Sleep(500)
	Switch $Format   
	Case "BMP(*.BMP)"

	Case "JPEG(*.JPG)"
		Send("j")
		Sleep(200)
		Send("{ENTER}")
		Sleep(500)
	Case "PaintBrush(*.PCX)"
		Send("p")
		Sleep(200)
		Send("{ENTER}")
		Sleep(500)
	Case "TIFF-uncompressed(*.TIF)"
		Send("t")
		Sleep(200)
		Send("{ENTER}")
		Sleep(500)
	Case "TIFF-packbits(*.TIF)"
		For $i = 0 to 1
			Send("t")
		Next
		Sleep(200)
		Send("{ENTER}")
		Sleep(500)
	Case "TIFF-G3 compression(*.TIF)"
		For $i = 0 to 2
			Send("t")
			Sleep(150)
		Next
		Sleep(200)
		Send("{ENTER}")
		Sleep(500)
	Case "TIFF-G4 compression(*.TIF)"
		For $i = 0 to 3
			Send("t")
			Sleep(150)
		Next
		Sleep(200)
		Send("{ENTER}")
		Sleep(500)
	Case "TIFF-multiple page(*.TIF)"
		For $i = 0 to 4
			Send("t")
			Sleep(150)
		Next
		Sleep(200)
		Send("{ENTER}")
		Sleep(500)
	Case "JPEG(*.JPG)"
		Send("j")
		Sleep(200)
		Send("{ENTER}")
		Sleep(500)
	Case "PICT(*.PCT)"
		Send("p")
		Send("p")
		Sleep(200)
		Send("{ENTER}")
		Sleep(500)
	Case "Targa(*.TGA)"
		For $i = 0 to 5
			Send("t")
			Sleep(150)
		Next
		Sleep(200)
		Send("{ENTER}")
		Sleep(500)
	Case "Targa-rle(*.TGA)"
		For $i = 0 to 6
			Send("t")
			Sleep(150)
		Next
		Sleep(200)
		Send("{ENTER}")
		Sleep(500)
	Case "PNG(*.PNG)"
		For $i = 0 to 2
			Send("p")
			Sleep(150)
		Next
		Sleep(200)
		Send("{ENTER}")
		Sleep(500)
	Case "Photoshop(*.PSD)"
		For $i = 0 to 3
			Send("p")
			Sleep(150)
		Next
		Sleep(200)
		Send("{ENTER}")
		Sleep(500)
	Case "GIF(*.GIF)"
		Send("g")
		Sleep(200)
		Send("{ENTER}")
		Sleep(500)
	Case "Text(*.TXT)"
		For $i = 0 to 7
			Send("t")
			Sleep(150)
		Next
		Sleep(200)
		Send("{ENTER}")
		Sleep(500)
	Case "Rich Text Format(*.RTF)"
		Send("r")
		Sleep(200)
		Send("{ENTER}")
		Sleep(500)
	Case "HTML(*.HTM)"
		Send("h")
		Sleep(200)
		Send("{ENTER}")
		Sleep(500)
	EndSwitch
EndFunc